Cell Therapy Clinical Trials
  • Home
  • About

On this page

  • Load packages
  • Load and format ADSL dataset
  • View table
    • This ADSL dataset was generated from CDISC generator website (cdiscdataset.com)
  • Demographics Table
    • rtables
    • flextable

DemographicsTable

CDISC
TLG
Demographics
Author

Karl Marquez

Published

September 7, 2026

This code describes how to generate a basic demographics table from an oncology clinical trial using a random dataset generator to analyze ADSL.

Load packages

Show Setup Code
# 1. Define required packages
packages <- c("tidyverse", "dplyr", "lubridate", "plotly",
              "ggstatsplot", "corrplot", "Hmisc", "flextable", "DT",
              "ggrepel", "ggforce", "ggpubr", "tern")

# 2. Check and install missing packages with an explicit CRAN mirror
missing_packages <- packages[!packages %in% rownames(installed.packages())]
if (length(missing_packages) > 0) {
  install.packages(missing_packages, repos = "[https://cloud.r-project.org](https://cloud.r-project.org)")
}

# 3. Load all libraries quietly
invisible(lapply(packages, library, character.only = TRUE))

Load and format ADSL dataset

Show Setup Code
adsl_df <- read.csv("adsl.csv", stringsAsFactors = FALSE)
#colnames(adsl_df)
#View(adsl_df)
#colnames(adsl)
adsl <- adsl_df %>% 
  mutate(
    SEX = factor(case_when(
      SEX == "M" ~ "Male",
      SEX == "F" ~ "Female"
    )),
    AGEGR1 = factor(case_when(
      between(AGE, 18, 40) ~ "18-40",
      between(AGE, 41, 64) ~ "41-64",
      AGE > 64 ~ ">=65"
    ))
  )

View table

This ADSL dataset was generated from CDISC generator website (cdiscdataset.com)

Show Setup Code
adsl.table <- adsl %>% 
  datatable(
    filter = list(position = "top", clear = FALSE),
    options = list(columnDefs = list(list(className = "dt_center", targets = "_all")),
                   scrollX = TRUE),
          caption = "ADSL from CDISC Generator"
        )
adsl.table

Demographics Table

rtables

Show Setup Code
vars <- c("AGE", "AGEGR1", "SEX", "ETHNIC", "RACE")
vars.label <- c(
  "Age (yr)", "Age Group", "Sex", "Ethnicity", "Race"
)

result <- basic_table(show_colcounts = TRUE) %>% 
  split_cols_by(var = "ACTARM") %>% 
  add_overall_col("All patients") %>% 
  analyze_vars(
    vars = vars,
    vars_label = vars.label
  ) %>% 
  build_table(adsl)
result
                                     Treatment 3   Treatment 2   Treatment 1   All patients
                                       (N=37)        (N=30)        (N=33)        (N=100)   
———————————————————————————————————————————————————————————————————————————————————————————
AGE                                                                                        
  n                                      37            30            33            100     
  Mean (SD)                          55.8 (16.3)   46.5 (18.4)   46.2 (21.1)   49.8 (19.0) 
  Median                                52.0          48.5          42.0           49.0    
  Min - Max                          26.0 - 80.0   21.0 - 79.0   18.0 - 79.0   18.0 - 80.0 
AGEGR1                                                                                     
  n                                      37            30            33            100     
  >=65                               12 (32.4%)     5 (16.7%)     9 (27.3%)      26 (26%)  
  18-40                               6 (16.2%)     12 (40%)     14 (42.4%)      32 (32%)  
  41-64                              19 (51.4%)    13 (43.3%)    10 (30.3%)      42 (42%)  
SEX                                                                                        
  n                                      37            30            33            100     
  Female                             21 (56.8%)     15 (50%)     17 (51.5%)      53 (53%)  
  Male                               16 (43.2%)     15 (50%)     16 (48.5%)      47 (47%)  
ETHNIC                                                                                     
  n                                      37            30            33            100     
  HISPANIC OR LATINO                 22 (59.5%)    11 (36.7%)    11 (33.3%)      44 (44%)  
  NOT HISPANIC OR LATINO             15 (40.5%)    19 (63.3%)    22 (66.7%)      56 (56%)  
RACE                                                                                       
  n                                      37            30            33            100     
  AMERICAN INDIAN OR ALASKA NATIVE    5 (13.5%)     4 (13.3%)     6 (18.2%)      15 (15%)  
  ASIAN                              11 (29.7%)    10 (33.3%)     8 (24.2%)      29 (29%)  
  BLACK OR AFRICAN AMERICAN           8 (21.6%)      6 (20%)      8 (24.2%)      22 (22%)  
  OTHER                               9 (24.3%)     4 (13.3%)     6 (18.2%)      19 (19%)  
  WHITE                               4 (10.8%)      6 (20%)      5 (15.2%)      15 (15%)  

flextable

Show Setup Code
flextable.demographics <- as_flextable(result)
flextable.demographics <- flextable.demographics %>%
  theme_booktabs() %>% 
  autofit()
flextable.demographics

Treatment 3

Treatment 2

Treatment 1

All patients

(N=37)

(N=30)

(N=33)

(N=100)

AGE

n

37

30

33

100

Mean (SD)

55.8 (16.3)

46.5 (18.4)

46.2 (21.1)

49.8 (19.0)

Median

52.0

48.5

42.0

49.0

Min - Max

26.0 - 80.0

21.0 - 79.0

18.0 - 79.0

18.0 - 80.0

AGEGR1

n

37

30

33

100

>=65

12 (32.4%)

5 (16.7%)

9 (27.3%)

26 (26%)

18-40

6 (16.2%)

12 (40%)

14 (42.4%)

32 (32%)

41-64

19 (51.4%)

13 (43.3%)

10 (30.3%)

42 (42%)

SEX

n

37

30

33

100

Female

21 (56.8%)

15 (50%)

17 (51.5%)

53 (53%)

Male

16 (43.2%)

15 (50%)

16 (48.5%)

47 (47%)

ETHNIC

n

37

30

33

100

HISPANIC OR LATINO

22 (59.5%)

11 (36.7%)

11 (33.3%)

44 (44%)

NOT HISPANIC OR LATINO

15 (40.5%)

19 (63.3%)

22 (66.7%)

56 (56%)

RACE

n

37

30

33

100

AMERICAN INDIAN OR ALASKA NATIVE

5 (13.5%)

4 (13.3%)

6 (18.2%)

15 (15%)

ASIAN

11 (29.7%)

10 (33.3%)

8 (24.2%)

29 (29%)

BLACK OR AFRICAN AMERICAN

8 (21.6%)

6 (20%)

8 (24.2%)

22 (22%)

OTHER

9 (24.3%)

4 (13.3%)

6 (18.2%)

19 (19%)

WHITE

4 (10.8%)

6 (20%)

5 (15.2%)

15 (15%)

Back to top
 

Karl Marquez